package com.gx.security.Symmetric_encryption;

import org.apache.commons.codec.binary.Hex;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.DESedeKeySpec;
import java.security.Key;
import java.security.SecureRandom;
import java.security.Security;
import java.security.spec.KeySpec;

/**
 * Created by gx on 2017/3/28.
 */
public class Imoc3DESTest {
    private static String src = "imooc security 3DES";

    public static void main(String[] args) {
        jdk3DES();
        bc3DES();
    }

    public static void jdk3DES() {
        try {

            //初始化key  生成器                                          指定Provider(提供者)
            KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede");
            //初始化key生成长度
//            keyGenerator.init(168);
//            两种初始化        生成默认长度key
            keyGenerator.init(new SecureRandom());
            SecretKey secretKey = keyGenerator.generateKey();
            //            拿到key
            byte[] byteskey = secretKey.getEncoded();
            KeySpec desKeySpec = new DESedeKeySpec(byteskey);
//            实例化key工厂
//             输入参数指定加密方式
            SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
//            获得转化之后的key
            Key convertSecretKey = factory.generateSecret(desKeySpec);
            //加密                          //加解密算法/工作模式/数据填充方式
            Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
            //加密模式           转化后key值
            cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
            //执行加密
            byte[] result = cipher.doFinal(src.getBytes());
//                                                            可以16进制也可以base64
            System.out.println("jdk 3DES 加密结果encrypt:" + Hex.encodeHexString(result));

            //解密操作       机密模式
            cipher.init(Cipher.DECRYPT_MODE, convertSecretKey);

//            拿到原始的result 解密
            result = cipher.doFinal(result);
            System.out.println("jdk 3DES 解密结果decrypt:" + new String(result));

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void bc3DES() {
        try {
            Security.addProvider(new BouncyCastleProvider());

            //初始化key  生成器                                          指定Provider(提供者)
            KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede","BC");
            //初始化key生成长度
//            keyGenerator.init(168);
//            两种初始化        生成默认长度key
            keyGenerator.init(new SecureRandom());
            SecretKey secretKey = keyGenerator.generateKey();
            //            拿到key
            byte[] byteskey = secretKey.getEncoded();
            KeySpec desKeySpec = new DESedeKeySpec(byteskey);
//            实例化key工厂
//             输入参数指定加密方式
            SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
//            获得转化之后的key
            Key convertSecretKey = factory.generateSecret(desKeySpec);
            //加密                          //加解密算法/工作模式/数据填充方式
            Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
            //加密模式           转化后key值
            cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
            //执行加密
            byte[] result = cipher.doFinal(src.getBytes());
//                                                            可以16进制也可以base64
            System.out.println("bc 3DES 加密结果encrypt:" + Hex.encodeHexString(result));

            //解密操作       机密模式
            cipher.init(Cipher.DECRYPT_MODE, convertSecretKey);

//            拿到原始的result 解密
            result = cipher.doFinal(result);
            System.out.println("bc 3DES 解密结果decrypt:" + new String(result));

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}